package com.yox.rpc.registry;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.yox.rpc.enumeration.RpcError;
import com.yox.rpc.exception.RpcException;
import com.yox.rpc.util.NacosUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;

/**
 * Nacos 服务注册中心
 *
 * 更多api请参数：https://nacos.io/zh-cn/docs/open-api.html
 */
public class NacosServiceRegistry implements ServiceRegistry{

    private static final Logger logger = LoggerFactory.getLogger(NacosServiceRegistry.class);

    /**
     * 将服务的名称和地址注册进服务注册中心
     * @param serviceName 服务名称
     * @param ip 提供服务的地址
     */
    @Override
    public void register(String serviceName, InetSocketAddress ip) {
        try {
            // 向Nacos注册服务
            NacosUtil.registerService(serviceName, ip);
        } catch (NacosException e) {
            logger.error("注册服务时发生错误", e);
            throw new RpcException(RpcError.REGISTRY_SERVICE_FAILED);
        }
    }
}
